//
//  musicTool.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/9/17.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "MusicTool.h"
#import "ZZMusicListGroup.h"
#import "ZZMusicList.h"
#import "ZZMusicModel.h"
#define musicListPath @"musicList.plist"
#define kListModelKey @"listModelKey"
#define kMusicCellIndetifier @"musicCellIndetifier"

@implementation MusicTool

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    
    static MusicTool *listGroupModel = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        listGroupModel = [super allocWithZone:zone];
        
    });
    
    return listGroupModel;
}

- (ZZMusicListGroup *)listModels {
    
    if (_listModels == nil) {
        
        // 从数据中拿
        NSString *path = NSHomeDirectory();
        path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",musicListPath]];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        //创建反归档对象
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:myData];
        //反归档
        ZZMusicListGroup* listGroupModel = [unarchiver decodeObjectForKey:kListModelKey];
        //完成反归档
        [unarchiver finishDecoding];
        
        if(listGroupModel == nil) { // 如果没有则数据初始化
            
            listGroupModel = [[ZZMusicListGroup alloc] init];
            
            ZZMusicList *musicList = [ZZMusicList new];
            musicList.listName = NSLocalizedString(@"MUSIC_MuscicList_DefalutSection", nil);
            musicList.musicList = [NSMutableArray array];
            musicList.isShow = NO;
            
            ZZMusicList *defalutList = [ZZMusicList new];
            defalutList.listName = NSLocalizedString(@"MUSIC_MuscicList_NotGroup", nil);
            defalutList.musicList = [NSMutableArray array];
            defalutList.isShow = YES;
            defalutList.signName = NSLocalizedString(@"MUSIC_MuscicList_NotGroup",nil);
            
            [listGroupModel.listgroup addObject:musicList];
            [listGroupModel.listgroup addObject:defalutList];
            // 归档
            //准备路径:
            NSString *path = NSHomeDirectory();
            NSLog(@"%@",path);
            
            path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",musicListPath]];
            //1:准备存储数据的对象
            NSMutableData *data = [NSMutableData data];
            //2:创建归档对象
            NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
            //3:开始归档
            [archiver encodeObject:listGroupModel forKey:kListModelKey];
            //4:完成归档
            [archiver finishEncoding];
            //5:写入文件当中
            BOOL result = [data writeToFile:path atomically:YES];
            if (result) {
                
                // 写入成功
            }
        }
        _listModels = listGroupModel;
        
    }
    
    return _listModels;
}

- (void)addSeaction:(NSString *)name {
    
    ZZMusicList *list = [ZZMusicList new];
    list.listName = name;
    list.musicList = [NSMutableArray array];
    
    [self.listModels.listgroup addObject:list];
    
    ZZMusicList *defaultList = nil;
    for(ZZMusicList *list in  [MusicTool new].listModels.listgroup) {
        
        if ([list.signName isEqualToString:NSLocalizedString(@"MUSIC_MuscicList_NotGroup", nil)]) {
            
            defaultList =list;
        }
    }
    
    [self.listModels.listgroup removeObject:defaultList];
    [self.listModels.listgroup addObject:defaultList];

    // 保存归档
    [self save];
}

- (void)save {
    
    //准备路径:
    NSString *path = NSHomeDirectory();
    NSLog(@"%@",path);
    
    path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",musicListPath]];
    //1:准备存储数据的对象
    NSMutableData *data = [NSMutableData data];
    //2:创建归档对象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //3:开始归档
    [archiver encodeObject:self.listModels forKey:kListModelKey];
    //4:完成归档
    [archiver finishEncoding];
    //5:写入文件当中
    BOOL result = [data writeToFile:path atomically:YES];
    if (result) {
        
        // 写入成功
        
    }
}


- (void)removeMusic:(ZZMusicModel *)model musicList:(ZZMusicList *)musicList {
    
    NSInteger index = [self.listModels.listgroup indexOfObject:musicList];
    ZZMusicList *list =[self.listModels.listgroup objectAtIndex:index];
    
    [list.musicList removeObject:model];
    [self save];
}

- (void)moveModel:(ZZMusicModel *)model fromList:(ZZMusicList *)listMusic toList:(ZZMusicList *)toListModel {
    
    NSInteger fromIndex  = [self.listModels.listgroup indexOfObject:listMusic];
    [self.listModels.listgroup[fromIndex].musicList  removeObject:model];
    NSInteger toIndex  = [self.listModels.listgroup indexOfObject:toListModel];
    
    [self.listModels.listgroup[toIndex].musicList addObject:model];
    
    [self save];
    
}

@end

